package com.hqd.ch03.v23.core;

import java.util.Comparator;

public class OrderComparator implements Comparator<Object> {
    public final static OrderComparator INSTANCE = new OrderComparator();

    @Override
    public int compare(Object o1, Object o2) {
        return doCompare(o1, o2);
    }

    private int doCompare(Object o1, Object o2) {
        int i1 = getOrder(o1);
        int i2 = getOrder(o2);
        return Integer.compare(i1, i2);
    }

    private int getOrder(Object obj) {
        Integer order = null;
        if (obj != null) {
            if (obj instanceof Ordered) {
                return ((Ordered) obj).getOrder();
            }
        }
        return Ordered.LOWEST_PRECEDENCE;
    }

}
